package zl.ordermanager.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import zl.ordermanager.service.RepairService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.Repair;

@Controller
@RequestMapping("/repair")
public class RepairController extends BaseController {

	@Autowired
	private RepairService repairService;

	@RequestMapping("/showList")
	public String showList(HttpSession session, PageHelper pageHelper, Model model) {
		DataGrid<Repair> dataGrid = repairService.dataGrid(session, pageHelper);
		List<Repair> repairList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("repairList", repairList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);

		return "repairList";
	}

	@RequestMapping("/showAdd")
	public String showAdd(Model model) {
		return "repairAdd";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(Repair repair) {
		try {
			if (repair.getTakeFromCustomer() == null || repair.getTakeFromCustomer().isEmpty()) {
				repair.setTakeFromCustomerHandling("");

				repair.setRepairDatetime("");
				repair.setRepairHandling("");
				repair.setRepairRemarks("");

				repair.setTakeBackDatetime("");
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getRepairDatetime() == null || repair.getRepairDatetime().isEmpty()) {
				repair.setRepairHandling("");
				repair.setRepairRemarks("");

				repair.setTakeBackDatetime("");
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getTakeBackDatetime() == null || repair.getTakeBackDatetime().isEmpty()) {
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getReturnCustomerDatetime() == null || repair.getReturnCustomerDatetime().isEmpty()) {
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			}
			repairService.add(repair);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showEdit/{repairId}")
	public String showEdit(@PathVariable("repairId") Integer repairId, Model model) {
		Repair repair = repairService.get(repairId);

		model.addAttribute("repair", repair);

		return "repairEdit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(Repair repair) {
		try {
			if (repair.getTakeFromCustomer() == null || repair.getTakeFromCustomer().isEmpty()) {
				repair.setTakeFromCustomerHandling("");

				repair.setRepairDatetime("");
				repair.setRepairHandling("");
				repair.setRepairRemarks("");

				repair.setTakeBackDatetime("");
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getRepairDatetime() == null || repair.getRepairDatetime().isEmpty()) {
				repair.setRepairHandling("");
				repair.setRepairRemarks("");

				repair.setTakeBackDatetime("");
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getTakeBackDatetime() == null || repair.getTakeBackDatetime().isEmpty()) {
				repair.setTakeBackHandling("");

				repair.setReturnCustomerDatetime("");
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			} else if (repair.getReturnCustomerDatetime() == null || repair.getReturnCustomerDatetime().isEmpty()) {
				repair.setReturnCustomerHandling("");
				repair.setDeliveryNo("");
			}
			repairService.edit(repair);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}
		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/delete/{repairId}")
	public ModelAndView delete(@PathVariable("repairId") Integer repairId) {

		repairService.delete(repairId);

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showRepair/{repairId}")
	public String showRepair(@PathVariable("repairId") Integer repairId, Model model) {
		Repair repair = repairService.get(repairId);

		model.addAttribute("repair", repair);

		return "repairShow";
	}
}
